var video_type_id = 0; //视频类型id
var load_leng = 1600; //指定距离加载一次
var pageIndex = 1; //当前页数
var pages = 1; //总页数
var old_video_type_id = 0; //视频类型id
var new_mess_index = 0; //最新信息索引
var new_mess_images_transform = 0; //图片transform距离
var new_mess_setInterval = null; //间隔调用函数对象

$(document).ready(function() {
	get_user_mess_and_token();
	var url = location.search;
	video_type_id = url.substr(1);
	if (url.indexOf("//") != -1) {
		video_type_id = url.substring(1, url.indexOf("//"));
	}
	if (video_type_id == "")
		video_type_id = 0;
	if (video_type_id != 0) { //有视频类型
		$("#first_video_type").attr("class",
			"tw-relative tw-whitespace-nowrap tw-inline-flex tw-items-center tw-text-16 tw-h-32 tw-text-common-gray tw-text-16"
		);
	}
	load_all_video_data();
});

/* 加载全部数据 */
function load_all_video_data() {
	$.post("http://127.0.0.1:8899/api/consumer/PublicController/load_all_car_video_data", function(data) {
		/* 加载全部视频类别 */
		var all_video_type_data = "";
		$(data.all_video_type).each(function(index, item) {
			all_video_type_data += "<div class=\"tw-float-left tw-relative\">" +
				"<div class=\"" + (video_type_id == item.video_type_id ?
					"tw-relative tw-whitespace-nowrap tw-inline-flex tw-items-center tw-text-16 tw-text-common-black tw-text-18 tw-font-medium top-nav_active__3WqwX tw-h-32 tw-ml-28" :
					"tw-relative tw-whitespace-nowrap tw-inline-flex tw-items-center tw-text-16 tw-h-32 tw-ml-28 tw-text-common-gray tw-text-16"
				) + "\" onclick='switch_video_type(this," +
				item.video_type_id + ")'>" +
				"<a class=\"top-nav_link__cSCA4\" title=\"" + item.video_type_name +
				"\" href=\"javascript:void(0)\"><h1>" + item.video_type_name + "</h1></a>" +
				"</div>" +
				"</div>";
		});
		$("#all_video_type").append(all_video_type_data);
		prev_element = $("#all_video_type div:eq(0) div:eq(0)");

		var all_new_video_images_mess = "";
		var all_new_video_mess = "";
		/* 查询最新的七条视频信息 */
		$(data.all_new_video_mess).each(function(index, item) {
			all_new_video_images_mess +=
				"<div class=\"swiper-slide\" style=\"width: 838px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;\">" +
				"<a href=\"http://127.0.0.1:8899/html/car_video_broadcast.html?" + item.video_id +
				"\" target=\"_blank\" class=\"jsx-757317224 img-wrap\">" +
				"<div style=\"display:block;overflow:hidden;position:absolute;top:0;left:0;bottom:0;right:0;z-index:0\">" +
				"<img src=\"http://120.48.47.144:8899/images/" + item.video_images_url + "\" " +
				" width=\"792\" height=\"718\" style=\"position:absolute;top:0;left:0;bottom:0;right:0;box-sizing:border-box;padding:0;border:none;margin:auto;display:block;width:0;height:0;min-width:100%;max-width:100%;min-height:100%;max-height:100%;object-fit:cover\" />" +
				"</div>" +
				"<div class=\"jsx-757317224 slide-mask\"></div>" +
				"</a>" +
				"</div>";
			all_new_video_mess +=
				"<div class=\"jsx-757317224 text-item " + (index == 0 ? "active" : "") +
				"\" onmouseover=\"new_video_mess_mouseover(this," + index +
				");\" onmouseout = \"new_video_mess_mouseout(this);\">" +
				"<a href=\"http://127.0.0.1:8899/html/car_video_broadcast.html?" + item.video_id +
				"\" \target=\"_blank\" style=\"font-size:" + (index == 0 ? 22 : 16) + "px\" title=\"" +
				item
				.video_introduction +
				"\" " +
				"class=\"jsx-757317224 text line-2 tw-text-base md:tw-text-xl xl:tw-text-2xl\">" +
				item
				.video_introduction + "</a> " +
				"<div class=\"jsx-757317224 video-info\"> " +
				"<a href=\"http://127.0.0.1:8899/html/car_my_homepage.html?" + item.user.user_id +
				"\" " +
				"class=\"jsx-757317224 author-wrap\"> " +
				"<div class=\"jsx-2217341492 avatar_avatar-img__170Bh tw-text-0 tw-mr-8\"> " +
				"<div style=\"display:inline-block;max-width:100%;overflow:hidden;position:relative;z-index:0\" " +
				"class=\"avatar tw-rounded-full\"> " +
				"<div style=\"box-sizing:border-box;display:block;max-width:100%\"> " +
				"<img style=\"max-width:100%;display:block;margin:0;border:none;padding:0\" " +
				"src=\"data:image/svg+xml;utf-8,&lt;svg width=&quot;16&quot; height=&quot;16&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; version=&quot;1.1&quot;/&gt;\" " +
				"aria-hidden=\"true\" role=\"presentation\" /> " +
				"</div><noscript> " +
				"<img src=\"//p3.dcarimg.com/img/user-avatar/b7865b3b34afaf93dba914fafaeec493~32x0.image\" " +
				"alt=\"头像\" width=\"20\" height=\"20\" " +
				"style=\"position:absolute;top:0;left:0;bottom:0;right:0;box-sizing:border-box;padding:0;border:none;margin:auto;display:block;width:0;height:0;min-width:100%;max-width:100%;min-height:100%;max-height:100%;object-fit:cover\"/></noscript> " +
				"<img src=\"http://120.48.47.144:8899/images/" + item.user.user_head_portrait_url +
				"\" " +
				"alt=\"头像\" width=\"20\" height=\"20\" " +
				"style=\"position:absolute;top:0;left:0;bottom:0;right:0;box-sizing:border-box;padding:0;border:none;margin:auto;display:block;width:0;height:0;min-width:100%;max-width:100%;min-height:100%;max-height:100%;object-fit:cover\" /> " +
				"</div> " +
				"</div> " +
				"<div title=\"" + item.user.user_name + "\" class=\"jsx-757317224 name line-1\">" + item
				.user.user_name + "</div> " +
				"</a> " +
				"<a href=\"http://127.0.0.1:8899/html/car_video_broadcast.html?" + item.video_id  + "\" target=\"_blank\" " +
				"class=\"jsx-757317224 stats\"> " +
				"<span class=\"jsx-757317224 play-num line-1\">" + (item.video_broadcast_count < 10000 ?
					item.video_broadcast_count : (item.video_broadcast_count / 10000).toFixed(2) + "万"
				) +
				"次播放</span> " +
				"<span class=\"jsx-757317224 publish-time line-1\">" + item.video_upload_date +
				"</span> " +
				"</a> " +
				"</div> " +
				"</div>";
		});
		$(".swiper-wrapper").append(all_new_video_images_mess);
		$("#all_new_video_mess").append(all_new_video_mess);

		/* 加载全部视频 ：默认查询全部*/
		load_all_video(video_type_id);
		new_mess_setInterval = setInterval(each_switch_show_mess, 2000);
	}, "json");
}

/* 加载全部视频 */
function load_all_video(video_type_id) {
	old_video_type_id = video_type_id;
	$.post("http://127.0.0.1:8899/api/consumer/VideoController/select_all_video_program/" + pageIndex + "/" +
		video_type_id,
		function(data) {
			var all_video_data = "";
			$(data.Pagedata).each(function(index, item) {
				all_video_data += "<div class=\"jsx-3689907623 tw-col-span-3 xl:tw-col-span-2 \"> " +
					"<div class=\"common-card_wrapper__Inr_n tw-pb-24\"><a target=\"_blank\" " +
					"class=\"jsx-1095092229\" href=\"http://127.0.0.1:8899/html/car_program_all_video.html?" +
					item.program.program_id +
					"\" target=\"_blank\"> " +
					"<div " +
					"class=\"tw-h-30 tw-flex tw-items-center tw-bg-white tw-px-8 tw-text-12 tw-text-common-gray tw-rounded-tl-2 tw-rounded-tr-2 hover:tw-bg-common-black hover:tw-text-common-white\"> " +
					"<p class=\"tw-flex-1 line-1 tw-mr-6\" style=\"color:inherit\"><span " +
					"title=\"" + item.program.program_name + "\">" + item.program.program_name +
					"</span></p> " +
					"<div>共 " + item.program.video_total +
					"<!-- -->期<i class=\"DCD_Icon icon_triangle_right\" " +
					"style=\"font-size:12px\"></i> " + "</div> " +
					"</div> " +
					"</a> " +
					"<div> " +
					"<div class=\"tw-relative tw-text-common-black\" style=\"font-size:0\"><a " +
					"title=\"" + item.video_introduction + "\" " +
					"href=\"http://127.0.0.1:8899/html/car_video_broadcast.html?" + item.video_id + "\" target=\"_blank\" " +
					"\" target=\"_blank\" " +
					"class=\"jsx-1095092229\"> " +
					"<div style=\"display:block;overflow:hidden;position:relative;z-index:0\" " +
					"class=\"tw-rounded-bl-2 tw-rounded-br-2 g-load-img-wrap\"> " +
					"<div " +
					"style=\"display:block;box-sizing:border-box;padding-top:56.00000000000001%\"> " +
					"</div><img " +
					"src=\"http://120.48.47.144:8899/images/" + item.video_images_url + "\" " +
					"alt=\"" + item.video_introduction + "\" width=\"250\" " +
					"height=\"140\" " +
					"style=\"position:absolute;top:0;left:0;bottom:0;right:0;box-sizing:border-box;padding:0;border:none;margin:auto;display:block;width:0;height:0;min-width:100%;max-width:100%;min-height:100%;max-height:100%;object-fit:cover\" " +
					"class=\"view-image_zoom__3b9VV\" /><img " +
					"src=\"\" " +
					"alt=\"" + item.video_introduction + "\" width=\"250\" " +
					"height=\"140\" " +
					"style=\"position:absolute;top:0;left:0;bottom:0;right:0;box-sizing:border-box;padding:0;border:none;margin:auto;display:block;width:0;height:0;min-width:100%;max-width:100%;min-height:100%;max-height:100%;object-fit:cover\" " +
					"class=\"zoom\" /> " +
					"</div> " +
					"<div " +
					"class=\"tw-w-50 tw-h-20 tw-flex tw-items-center tw-justify-center tw-rounded-2 tw-bg-black tw-bg-opacity-80 tw-text-white tw-text-12 tw-absolute tw-right-8 tw-bottom-8 tw-z-10\"> " +
					"<span class=\"font-size-10_wrapper__pmvsT undefined\" " +
					"style=\"width:44px;height:10px\"><span " +
					"class=\"font-size-10_inner__y3DQ4\"><i " +
					"class=\"DCD_Icon icon_play_1 tw-text-16 tw-mr-2\"></i>" + item.video_duration +
					"</span></span> " +
					"</div> " +
					"</a></div> " +
					"<h3 " +
					"class=\"tw-leading-22 tw-text-16 tw-font-medium tw-text-common-black tw-text-justify line-2 g-active-link-text tw-mt-12\"> " +
					"<a title=\"" + item.video_introduction + "\" " +
					"href=\"http://127.0.0.1:8899/html/car_video_broadcast.html?" + item.video_id + "\" target=\"_blank\" " +
					"class=\"jsx-1095092229\">" + item.video_introduction + "</a> " +
					"</h3> " +
					"<p class=\"tw-mt-8 tw-h-20 tw-lead tw-text-common-gray tw-text-14\"><a " +
					"title=\"" + item.video_introduction + "\" " +
					"href=\"http://127.0.0.1:8899/html/car_video_broadcast.html?" + item.video_id + "\" target=\"_blank\" " +
					"class=\"jsx-1095092229\"><span class=\"tw-mr-8\">" + (item.video_broadcast_count <
						10000 ? item.video_broadcast_count : (item.video_broadcast_count / 10000).toFixed(
							2) + "万") +
					"<!-- -->次 " +
					"<!-- -->播放 " +
					"</span><span>" + item.video_upload_date + "</span></a></p> " +
					"</div> " +
					"</div> " +
					"</div>";
			});
			pages = data.PageTotal;
			$("#all_video").append(all_video_data);
		}, "json");
}

/* 选中视频类型 */
function switch_video_type(element, video_type_id) {
	$(element).parent().siblings().find("div:eq(0)").attr("class",
		"tw-relative tw-whitespace-nowrap tw-inline-flex tw-items-center tw-text-16 tw-h-32 tw-ml-28 tw-text-common-gray tw-text-16"
	);
	$(element).attr("class",
		"tw-relative tw-whitespace-nowrap tw-inline-flex tw-items-center tw-text-16 tw-text-common-black tw-text-18 tw-font-medium top-nav_active__3WqwX tw-h-32 tw-ml-28"
	);
	if (video_type_id != 0) {
		$("#all_video_type div:eq(0) div:eq(0)").attr("class",
			"tw-relative tw-whitespace-nowrap tw-inline-flex tw-items-center tw-text-16 tw-h-32 tw-text-common-gray tw-text-16"
		);
	} else {
		$("#all_video_type div:eq(0) div:eq(0)").attr("class",
			"tw-relative tw-whitespace-nowrap tw-inline-flex tw-items-center tw-text-16 tw-text-common-black tw-text-18 tw-font-medium top-nav_active__3WqwX tw-h-32"
		);
	}
	pageIndex = 1;
	load_leng = 1600;
	$("#all_video div").remove();
	load_all_video(video_type_id);
}

/* 循环切换显示最新信息 */
function each_switch_show_mess() {
	new_mess_index++;
	new_mess_images_transform -= 838;
	if (new_mess_index == 0) {
		$(".swiper-slide").css("transform", "translate3d(0px,0px,0px)");
	}
	$("#all_new_video_mess>div:eq(" + new_mess_index + ")").find("a:eq(0)").css("font-size", "22px");
	$("#all_new_video_mess>div:eq(" + new_mess_index + ")").siblings("div").find("a:eq(0)").css("font-size", "16px");
	$("#all_new_video_mess>div:eq(" + new_mess_index + ")").attr("class", "jsx-757317224 text-item active");
	$("#all_new_video_mess>div:eq(" + new_mess_index + ")").siblings().attr("class", "jsx-757317224 text-item");
	$(".swiper-wrapper>div:eq(" + new_mess_index + ")").css("opacity", 1);
	$(".swiper-wrapper>div:eq(" + new_mess_index + ")").siblings().css("opacity", 0);
	$(".swiper-wrapper>div:eq(" + new_mess_index + ")").css("transform", "translate3d(" + new_mess_images_transform +
		"px,0px,0px)");
	if (new_mess_index == 6) {
		new_mess_index = -1;
		new_mess_images_transform = 838;
	}
}

/* 最新视频信息悬浮事件 */
function new_video_mess_mouseover(element, index) { //当前对象，悬浮div索引
	$(element).find("a:eq(0)").css("font-size", "22px");
	$(".swiper-slide").css("transform", "translate3d(0px,0px,0px)");
	clearInterval(new_mess_setInterval); //暂停
	new_mess_index = index - 1;
	new_mess_images_transform = -(index * 838) + 838;
	each_switch_show_mess();
}

/* 最新视频信息悬浮事件 */
function new_video_mess_mouseout(element) { //当前对象
	new_mess_setInterval = setInterval(each_switch_show_mess, 2000);
}

/* 懒加载刷新页面 */
window.onscroll = function() {
	var scroll_top = document.body.scrollTop || document.documentElement.scrollTop;
	var win_top = window.innerHeight;
	if (scroll_top + win_top > load_leng && pageIndex != pages) {
		pageIndex++;
		load_all_video(old_video_type_id);
		load_leng += 1000;
	}
}
